<template>
    <div class="register-container">
      <div class="header">
        <h1 class="title">智慧打卡</h1>
        <!-- <div class="subtitle">
          <h2>创建用户</h2>
          <p>输入账户密码进入系统</p>
        </div> -->
      </div>
  
      <div class="card">
        <form @submit.prevent="handleSubmit" class="form-section">
        <InputField
          v-model="form.username"
          label=""
          placeholder="请输入用户名"
          required
          @blur="v$.username.$touch()"
          :error-message="v$.username.$errors[0]?.$message"
          :error="v$.username.$errors[0]?.$message || false"
        />
  
        <InputField
          type="password"
          v-model="form.password"
          label=""
          placeholder="请输入密码"
          required
          @blur="v$.password.$touch()"
          :error-message="v$.password.$errors[0]?.$message"
          :error="v$.password.$errors[0]?.$message || false"
        />
  
        <InputField
          type="password"
          v-model="form.confirmPassword"
          label=""
          placeholder="请再次输入密码"
          required
          @blur="v$.confirmPassword.$touch()"
          :error-message="v$.confirmPassword.$errors[0]?.$message"
          :error="v$.confirmPassword.$errors[0]?.$message || false"
        />
  
          <button type="submit" class="primary-btn">注册</button>
  
          <div class="divider">
            <span class="divider-line"></span>
          </div>
  
          <p class="footer-text">
            已经有账号了么？
            <Link href="/user/login" class="link">去登录吧</Link>
          </p>
        </form>
      </div>
    </div>

  </template>
  
  <script setup>
  import { reactive, computed } from 'vue';
  import { onMounted, ref } from 'vue';
  import useVuelidate from '@vuelidate/core';
  import { required, alpha, minLength, sameAs, helpers } from '@vuelidate/validators';
  import request from '@/utils/request';
  import apiUrls  from '@/config/apiUrls'; 
  import { router, Link } from '@inertiajs/vue3'; 
  import {baseNotifyInfo} from '@/utils/utils.js';
  import {StatusCodeEnum} from '@/enum/StatusCodeEnum.js';

  const form = reactive({
    username: '',
    password: '',
    confirmPassword: ''
  });

  const password = computed(() => form.password)
  const rules = {
    username: {
      required: helpers.withMessage('用户名不能为空', required),
      minLength: helpers.withMessage('至少3个字符', minLength(3)),
      alpha: helpers.withMessage('只能英文字母或数字组合', helpers.regex(/^[a-zA-Z0-9]+$/))
    },
    password: {
      required: helpers.withMessage('密码不能为空', required),
      minLength: helpers.withMessage('至少6个字符', minLength(6))
    },
    confirmPassword: {
      required: helpers.withMessage('确认密码不能为空', required),
      sameAsPassword: helpers.withMessage(
        '两次输入的密码不一致',
        sameAs(password) // 关键验证规则
      )
    }
  };

  const v$ = useVuelidate(rules, form);
  
  const handleSubmit = async () => {
    const isValid = await v$.value.$validate();
    if (!isValid) return;
    regiserUser()
    console.log("注册");
  };


    // 用户登录
    const regiserUser = async () => {
      
      const response = await request.post(apiUrls.users.register, form, {
        headers: {
          'Content-Type': 'application/json'
        }
      });
      
      if (response.code === 200){
        baseNotifyInfo(StatusCodeEnum.insert_success(response.message))
      }else{
        baseNotifyInfo({ title:"登录提示", content:response.message, type:2 })
      }
  };
  
</script>    
  <style scoped>
  .register-container {
    min-height: 100vh;
    padding: 2rem;
    background: #f5f7fb;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  
  .header {
    text-align: center;
    margin-bottom: 2.5rem;
  }
  
  .title {
    font-size: 2rem;
    color: #1a1a1a;
    margin-bottom: 0.5rem;
    font-weight: 600;
  }
  
  .subtitle h2 {
    font-size: 1.25rem;
    color: #333;
    margin-bottom: 0.5rem;
  }
  
  .subtitle p {
    color: #666;
    font-size: 0.875rem;
  }
  
  .card {
    background: white;
    border-radius: 12px;
    padding: 2rem;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    width: 100%;
    max-width: 400px;
  }
  
  .input-group {
    margin-bottom: 1.5rem;
    position: relative;
  }
  
  .modern-input {
    width: 100%;
    padding: 0.875rem;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    font-size: 0.9375rem;
    transition: all 0.2s;
  }
  
  .modern-input:focus {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
    outline: none;
  }
  
  .input-error {
    border-color: #ff4d4f;
  }
  
  .error-msg {
    color: #ff4d4f;
    font-size: 0.75rem;
    position: absolute;
    bottom: -1.25rem;
    left: 0;
  }
  
  .primary-btn {
    width: 100%;
    padding: 0.875rem;
    background: #000000;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
  }
  
  .primary-btn:hover {
    background: #7e81816b;
  }
  
  .divider {
    margin: 2rem 0;
    border-top: 1px solid #eee;
  }
  
  .footer-text {
    text-align: center;
    color: #666;
    margin-top: 1.5rem;
  }
  
  .link {
    color: #4f46e5;
    text-decoration: none;
    font-weight: 500;
  }
  
  .link:hover {
    text-decoration: underline;
  }
  </style>